package bat.ke.qq.com.core;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 源码学院-Fox
 * 只为培养BAT程序员而生
 * http://bat.ke.qq.com
 * 往期视频加群:516212256 暗号:6
 *
 * 请求分发器
 */
@WebServlet(urlPatterns="/api")
public class ApiGatewayServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    ApplicationContext context;
    private ApiGatewayHandler apiHandler;

    @Override
    public void init() throws ServletException {
        super.init();
        // 拿到ioc容器
        context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        apiHandler = context.getBean(ApiGatewayHandler.class);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        apiHandler.handle(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        apiHandler.handle(req, resp);
    }
}
